package itdep.common.domain.hibernate.type;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.type.LocaleType;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Locale;

/**
 * @author Ivan Khalopik
 * @version $Revision: 4 $ $Date: 2010-06-10 13:13:16 +0000 (Thu, 10 Jun 2010) $
 */
public class LocaleTypeEx extends LocaleType {
	public static final String DEFAULT_LOCALE_VALUE = "default";

	@Override
	public Object fromStringValue(final String string) {
		if (DEFAULT_LOCALE_VALUE.equals(string)) {
			return Locale.ROOT;
		}
		return super.fromStringValue(string);
	}

	@Override
	public void set(final PreparedStatement st, final Object value, final int index) throws HibernateException, SQLException {
		if (Locale.ROOT.equals(value)) {
			Hibernate.STRING.set(st, DEFAULT_LOCALE_VALUE, index);
		} else {
			super.set(st, value, index);
		}
	}
}
